package cz.cuni.mff.ufal.volk.events;

import org.apache.log4j.Logger;

import cz.cuni.mff.ufal.volk.NabaztagProxy;
import cz.cuni.mff.ufal.volk.Speech;
import cz.cuni.mff.ufal.volk.Text;
import cz.cuni.mff.ufal.volk.command.MessageCommand;
import cz.cuni.mff.ufal.volk.message.ChoreographyBuilder;
import cz.cuni.mff.ufal.volk.nabaztag.Ear;
import cz.cuni.mff.ufal.volk.nabaztag.Led;
import cz.cuni.mff.ufal.volk.nabaztag.RotationDirection;
import cz.cuni.mff.ufal.volk.services.Services;
import cz.cuni.mff.ufal.volk.services.TextToSpeech;

/**
 * A sample NabaztagEventListener whose only purpose is to log all events.
 *
 * @author Bartłomiej Etenkowski
 */
public class BasicNabaztagEventListener implements NabaztagEventListener {

	private final static Logger log = Logger.getLogger(BasicNabaztagEventListener.class);

	@Override
  public void onStarted(NabaztagProxy sender) {
		log.info(String.format("%s#onStarted", sender.getSerial()));
		sender.setPingInterval(1);
		//sender.message(new MessageCommand(new SetLedColorMessage(Led.CENTER, 100, 100, 100)));
		MessageCommand command = new MessageCommand();
		command.setLedColor(Led.CENTER, 100, 100, 100);
		command.waitForPreviousCommand();
		sender.message(command);
  }

	@Override
  public void onClick(NabaztagProxy sender) {
		log.info(String.format("%s#onClick", sender.getSerial()));
//		TextToSpeech tts = (TextToSpeech)Services.registered().get(Services.TEXT_TO_SPEECH);
//		Speech s = tts.process(new Text("cs", "zmáčkl jsi tlačítko"), null);
//		sender.playSound(s);
		ChoreographyBuilder ch = new ChoreographyBuilder();
		ch.appendTempo(100);
		ch.appendEarPosition(0, Ear.LEFT, 0, RotationDirection.FORWARD);
		ch.appendEarPosition(6, Ear.LEFT, 5, RotationDirection.BACKWARD);
		ch.appendEarPosition(6, Ear.LEFT, 0, RotationDirection.BACKWARD);
		//ch.appendEarPosition(Ear.LEFT, 0, RotationDirection.FORWARD);
		ch.appendLed(0, Led.CENTER, 0, 255, 255);
		ch.appendLed(1, Led.LEFT, 255, 0, 255);
		ch.appendLed(1, Led.RIGHT, 255, 255, 0);
		ch.appendLed(1, Led.NOSE, 255, 0, 0);
		ch.appendLed(1, Led.BOTTOM, 100, 100, 100);
		ch.appendRandomMidi(0);
		//ch.appendEarPosition(10, Ear.RIGHT, 0, RotationDirection.BACKWARD);
		ch.appendRandomMidi(5);
		sender.playChoreography(ch.getBytes());
  }

	@Override
  public void onDoubleClick(NabaztagProxy sender) {
		log.info(String.format("%s#onDoubleClick", sender.getSerial()));
		ChoreographyBuilder ch = new ChoreographyBuilder();
		//ch.appendTempo(5);
		ch.appendEarPosition(0, Ear.LEFT, 5, RotationDirection.FORWARD);
		ch.appendEarPosition(0, Ear.RIGHT, 8, RotationDirection.BACKWARD);
		ch.appendRandomMidi(1);
		ch.appendLed(0, Led.CENTER, 50, 50, 50);
		sender.playChoreography(ch.getBytes());
  }

	@Override
  public void onSoundRecorded(NabaztagProxy sender, Speech s) {
		log.info(String.format("%s#onSoundRecorder (%d bytes)", sender.getSerial(), s.getData().size()));
  }

	@Override
  public void onEarsMoved(NabaztagProxy sender, int left, int right) {
		log.info(String.format("%s#onEarsMoved, left=%d, right=%d", sender.getSerial(), left, right));
  }

	@Override
	public void onVoiceInterrupted(NabaztagProxy sender) {
		log.info(String.format("%s#onVoiceInterrupted", sender.getSerial()));
		TextToSpeech tts = (TextToSpeech)Services.registered().get(Services.TEXT_TO_SPEECH);
		Speech s = tts.process(new Text("cs", "chm"), null);
		sender.playSound(s);
	}

	@Override
  public void onCompleted(NabaztagProxy sender) {
		log.info(String.format("%s#onCompleted", sender.getSerial()));
  }

	@Override
  public void onTimeout(NabaztagProxy sender) {
		log.info(String.format("%s#onTimeout", sender.getSerial()));
  }

}
